﻿/****************************************************************************************
 *
 *  Filename:       CommentSettingDlg.cs
 *  Description:    CommentSettingDlg form
 *  License: GPLv2: http://logsubstpol.codeplex.com/license
 *
 *  History
 *  -------
 *
 *    Date   | Author          | Comment
 *  ---------+-----------------+---------------------------------------------------------
 *  08.08.09 | jkalmbach_cp    | First version published on
 *           |                 | Codeplex: http://logsubstpol.codeplex.com/
 *  $log$
 *
 ****************************************************************************************
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LogSubstPol
{
  public partial class CommentSettingDlg : Form
  {
    public CommentSettingDlg()
    {
      InitializeComponent();
    }

    public bool CheckForNewConfig
    {
      get
      {
        return this.chkCheckForNewConfig.Checked;
      }
      set
      {
        this.chkCheckForNewConfig.Checked = value;
      }
    }

    public bool InsertBefore
    {
      get
      {
        return this.chkInsertBefore.Checked;
      }
      set
      {
        this.chkInsertBefore.Checked = value;
      }
    }

    public string[] FirstLines
    {
      get
      {
        if (this.chkHasFirstLines.Checked == false)
          return new string[0];
        string[] s = (string[])this.txtFirstLines.Lines.Clone();
        if (s.Length <= 0) s = new string[] { string.Empty };
        return s;
      }
      set
      {
        if ((value == null) || (value.Length <= 0))
        {
          this.txtFirstLines.Lines = new string[0];
          this.chkHasFirstLines.Checked = false;
        }
        else
        {
          this.txtFirstLines.Lines = (string[])value.Clone();
          this.chkHasFirstLines.Checked = true;
        }
      }
    }

    public string[] RepeatLines
    {
      get
      {
        return GetLines(this.txtRepeatLines.Lines);
      }
      set
      {
        if (value == null)
          this.txtRepeatLines.Lines = new string[0];
        else
          this.txtRepeatLines.Lines = (string[])value.Clone();
      }
    }

    public string[] EndLines
    {
      get
      {
        if (this.chkHasEndLines.Checked == false)
          return new string[0];
        string[] s = (string[])this.txtEndLines.Lines.Clone();
        if (s.Length <= 0) s = new string[] { string.Empty };
        return s;
      }
      set
      {
        if ( (value == null) || (value.Length <= 0) )
        {
          this.txtEndLines.Lines = new string[0];
          this.chkHasEndLines.Checked = false;
        }
        else
        {
          this.txtEndLines.Lines = (string[])value.Clone();
          this.chkHasEndLines.Checked = true;
        }
      }
    }

    public string[] Extensions
    {
      get
      {
        return GetLines(this.txtExtensions.Lines);
      }
      set
      {
        if (value == null)
          this.txtExtensions.Lines = new string[0];
        else
          this.txtExtensions.Lines = (string[])value.Clone();
      }
    }

    public UserNameMappingListe UserNameMappings
    {
      get
      {
        var u = new UserNameMappingListe();
        for (int i = 0; i < this.userMapping.RowCount; i++)
        {
          string un = this.userMapping.Rows[i].Cells[0].Value as string;
          string mn = this.userMapping.Rows[i].Cells[1].Value as string;
          if ( (un != null) && (mn != null))
            u.Add(new UserNameMapping() { UserName = un, MappingName = mn}); 
        }
        return u;
      }
      set
      {
        // Fülle die Tabelle mit den Werten...
        this.userMapping.RowCount = 1;
        if (value != null)
        {
          foreach (var v in value)
          {
            this.userMapping.Rows.Add(new object[] { v.UserName, v.MappingName });
          }
        }
      }
    }

    string[] GetLines(string[] org)
    {
      if (org == null) return new string[0];
      string[] s = (string[])org.Clone();
      if (s.Length <= 0) return s;
      if ((s.Length == 1) && ((s[0] == null) || (s[0].Length <= 0)))
        return new string[0];
      return s;
    }

    private void chkHasFirstLines_CheckedChanged(object sender, EventArgs e)
    {
      this.txtFirstLines.Enabled = chkHasFirstLines.Checked;
    }

    private void chkHasEndLines_CheckedChanged(object sender, EventArgs e)
    {
      this.txtEndLines.Enabled = chkHasEndLines.Checked;
    }
  }
}
